using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using Publish.Utility;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace Publish
{
    public partial class NewQuery : Form
    {
        #region Constructors

        public NewQuery()
        {
            InitializeComponent();

            SetFormSize();//设置窗体尺寸
        }

        #endregion

        #region Fields

        private SqlHelper sqlHelper = new SqlHelper();

        #endregion

        #region Event

        #region Form

        //窗体加载
        private void NewQuery_Load(object sender, EventArgs e)
        {

        }

        //窗体改变
        private void NewQuery_SizeChanged(object sender, EventArgs e)
        {
            SetFormSize();//设置窗体尺寸
        }

        //按键事件
        private void NewQuery_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 116)
            {
                Query();
            }
        }

        #endregion

        #region RichTextBox


        #endregion

        #region DataGridView

        //显示行号
        private void DgvRowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var dgv = (DataGridView)sender;

            var rectangle = new Rectangle(e.RowBounds.Location.X,
                 e.RowBounds.Location.Y,
                 dgv.RowHeadersWidth - 4,
                 e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dgv.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dgv.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        #endregion

        #endregion

        #region Method

        #region Private

        /// <summary>
        /// 设置窗体尺寸
        /// </summary>
        private void SetFormSize()
        {
            //Height = 700;
            //Width = 800;
            dgvResults.Height = Height - panel1.Height - 60;
            dgvResults.Width = Width - 15;
            dgvResults.Location = new Point(0, panel1.Height);
        }

        /// <summary>
        /// 实时查询
        /// </summary>
        private void Query()
        {
            var sql = richTextBox1.Text.Trim();

            if (!sql.ToUpperInvariant().Contains("INSERT") &&//阻止增删改语句执行
                !sql.ToUpperInvariant().Contains("DELETE") &&
                !sql.ToUpperInvariant().Contains("UPDATE") &&
                !sql.ToUpperInvariant().Contains("ALTER") &&
                !sql.ToUpperInvariant().Contains("TRUNCATE"))

                dgvResults.DataSource = sqlHelper.GetDataSet(sql);
        }

        #endregion

        #region Public



        #endregion

        #endregion
    }
}
